<template>
  <div>

    <div style="margin:2% 0;display: inline-block;">
<div style="position: relative;display: inline-block;margin: 10px 0px;">
      <span>商铺名称：</span>
      <el-input style="width: 200px;" @keydown.enter.native="shopSelect" placeholder="请输入商铺名称"
        v-model="shopName">
      </el-input>&nbsp;&nbsp
    </div>
      <span>商品标题：</span>
      <el-input style="width: 150px;" @keydown.enter.native="select" clearable placeholder="请输入商品标题" v-model="title">
      </el-input>&nbsp;&nbsp;&nbsp;&nbsp;
      <span>商品类型：</span>
      <el-select v-model="classifyId" class="margin15" placeholder="请选择商品类型" style="width:200px;" @change="handleChange()">
        <el-option v-for="(item,index) in classDatas" :key="index" :label="item.classifyName" :value="item.classifyId">
        </el-option>
      </el-select>&nbsp;&nbsp;&nbsp;&nbsp;
      <span>商品状态：</span>
      <el-select v-model="status" placeholder="请选择商品状态" style="width:150px;" @change="select(status)">
        <el-option v-for="item in statusmain" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
    <div style="display: inline-block;">
      <el-button style='margin-left:15px;' size="mini" type="primary" icon="document" @click="select">查询</el-button>
      <el-button style='margin-left:15px;' size="mini" type="primary" icon="document" @click="cleans">重置</el-button>
      <!-- <el-button style='margin-left:15px;' :disabled="!isAuth('shopAdmin:add')" size="mini" type="primary"
        icon="document" @click="addShop">发布商品</el-button> -->
        <el-button style='margin:0 0 20px 20px;' size="mini" type="primary" icon="document"
        	@click="shanxiajia()" :disabled="checkBoxData.length <= 0">批量上下架
        </el-button>
        <el-button style='margin:0 0 20px 20px;' size="mini" type="danger" icon="document"
        	@click="deletesPl()" :disabled="checkBoxData.length <= 0">批量删除
        </el-button>
        <el-button style='margin:0 0 20px 20px;' size="mini" type="primary" icon="document"
        	@click="kiucun()" :disabled="checkBoxData.length <= 0">批量改库存
        </el-button>
    </div>
    <el-table v-loading="tableDataLoading" :data="tableData.list" @selection-change="changeFun">
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column fixed prop="goodsId" label="编号" width="80">
      </el-table-column>
      <el-table-column label="商铺名称" prop="shopName" width="150">
      </el-table-column>
      <el-table-column fixed prop="goodsCover" label="商品封面图">
        <template slot-scope="scope">
          <img :src="scope.row.goodsCover" alt="" width="60" height="60">
        </template>
      </el-table-column>
      <el-table-column  prop="goodsCover" label="商品图" width="150">
        <template slot-scope="scope">
          <!-- <img :src="scope.row.goodsCover" alt="" width="60" height="60"> -->
          <div v-for="(item,index) in scope.row.goodsPicture" :key="index"
          	style="display: inline-block; margin: 3px;">
          	<el-popover placement="top-start" title="" trigger="hover">
          		<img style="width: 50px; height: 50px" :src="item" alt="" slot="reference">
          		<img style="width: 300px; height: auto" :src="item" alt="">
          	</el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="goodsName" label="商品类型">
        <template slot-scope="scope">
          <span v-if="scope.row.classifyName == null">暂无分类</span>
          <span v-else>{{scope.row.classifyName}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed prop="goodsName" label="商品标题" width="200">
      </el-table-column>
      <el-table-column prop="goodsDescribe" label="商品描述" width="300">
      </el-table-column>
      <el-table-column prop="originalMoney" label="商品原价">
        <template slot-scope="scope">
          <span>{{scope.row.originalMoney | numFilter}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsMoney" label="商品折扣价">
        <template slot-scope="scope">
          <span>{{scope.row.goodsMoney | numFilter}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="packMoney" label="打包费(元/个 具体按照数量计算)">
        <template slot-scope="scope">
          <span>{{scope.row.packMoney | numFilter}}元/个</span>
        </template>
      </el-table-column>
      <el-table-column prop="inventory" label="库存" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.inventory}}</span>
          <div style="position: relative;display: inline-block;">
            <el-input style="width: 160px;" @keydown.enter.native="kcselect(scope.row,scope.row.kucun)" clearable
              placeholder="请输入修改库存" v-model="scope.row.kucun">
            </el-input>
            <span @click="kcselect(scope.row,scope.row.kucun)" style="position: absolute;right: 18px;top:8px;">
              <icon-svg name="bianji" class="site-sidebar__menu-icon"></icon-svg>
            </span>
          </div>

        </template>
      </el-table-column>
     <el-table-column prop="goodsLabel" label="标签">
      </el-table-column>
      <el-table-column prop="salesVolume" label="商品销量">
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="150">
      				<template slot-scope="scope">
      					<el-input-number size="small" v-model="scope.row.sort" :min="0" @change="paixu(scope.row)">
      					</el-input-number>
      				</template>
      			</el-table-column>
      			<el-table-column prop="gsrStatus" label="库存状态" width="80">
      				<template slot-scope="scope">
      					<span v-if="scope.row.gsrStatus === 2 ">已售完</span>
      					<span v-else >剩余：{{scope.row.inventory}}</span>
      				</template>
      			</el-table-column>
      			<el-table-column prop="status" label="商品状态">
      				<template slot-scope="scope">
      					<el-switch v-model="scope.row.status"
      						@change="change(scope.row.status,scope.row.goodsId)"
      						:active-value="openValue" :inactive-value="closeValue" active-color="#13ce66"
      						inactive-color="#ff4949">
      					</el-switch>

      				</template>
      				<!-- <template slot-scope="scope">
        <span style="color: #4f9dec;cursor: pointer;" v-if="scope.row.status === 0 ">上架</span>
        <span style="color: #4f9dec;cursor: pointer;" v-if="scope.row.status === 1 ">下架</span>
      </template> -->
      			</el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" :disabled="!isAuth('shopAdmin:update')"
            @click="shopUpdates(scope.$index, scope.row)">修改
          </el-button>
          <el-button size="mini" type="danger" :disabled="!isAuth('shopAdmin:delete')" @click="shopdeletes(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center;margin-top: 10px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10,  20, 30, 50, 100]"
        :page-size="size" :current-page="page" layout="total,sizes, prev, pager, next,jumper"
        :total="tableData.totalCount">
      </el-pagination>
    </div>
    <!-- 批量上下架 -->
    <el-dialog title="批量上下架" customClass="customWidth" center :visible.sync="dialogFormVisibleSxj">
    	<div style="text-align: center;">
    		<span style="display: inline-block;text-align: right;">状态：</span>
    		<el-radio-group v-model="guigeType">
    			<el-radio :label="0">上架</el-radio>
    			<el-radio :label="1">下架</el-radio>
    		</el-radio-group>
    	</div>
    	<div slot="footer" class="dialog-footer">
    		<el-button @click="dialogFormVisibleSxj = false">取 消</el-button>
    		<el-button type="primary" @click="transferClcik()">确 定</el-button>
    	</div>
    </el-dialog>
    <!-- 批量改库存 -->
    <el-dialog title="批量修改库存" customClass="customWidth" center :visible.sync="dialogFormVisibleKc">
    	<div style="text-align: center;">
    		<span style="display: inline-block;text-align: right;">库存：</span>
    		<el-input style="width:50%;" v-model="num" placeholder="请输入库存"></el-input>
    	</div>
    	<div slot="footer" class="dialog-footer">
    		<el-button @click="dialogFormVisibleKc = false">取 消</el-button>
    		<el-button type="primary" @click="transferClcikKc()">确 定</el-button>
    	</div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        size: 10,
        page: 1,
        pagelist: [],
        id: '',
        img: '',
        title: '',
        type: '',
        typeId: '',
        readonly: true,
        merchants: '',
        price: '',
        memberPrice: '',
        originalPrice: '',
        sales: '',
        status: '',
        status2: '',
        descrition: '',
        totalnum: 0,
        imgUrl: '',
        linkUrl: '',
        classifyId: '',
        classDatas: [],
        statusmain: [{
            value: '',
            label: '全部'
          },
          {
            value: 0,
            label: '上架'
          },
          {
            value: 1,
            label: '下架'
          }
        ],
        statusmain2: [{
            value: 1,
            label: '上架'
          },
          {
            value: 2,
            label: '下架'
          }
        ],
        formLabelWidth: '200px',
        tableDataLoading: false,
        dialogFormVisible2: false,
        dialogFormVisible3: false,
        typeDatas: [],
        advertData: [],
        tableData: [],
        shopName:'',

        checkBoxData: [], //多选框选择的值
        dialogFormVisibleSxj: false,
        guigeType: '',
        num: '',
        dialogFormVisibleKc: false,
        openValue:0,
        closeValue: 1,
      }
    },
    filters: {
      numFilter(value) {
        let realVal = ''
        if (!isNaN(value) && value !== '') {
          // 截取当前数据到小数点后三位
          let tempVal = parseFloat(value).toFixed(3)
          realVal = tempVal.substring(0, tempVal.length - 1)
        } else {
          realVal = '--'
        }
        return realVal
      }
    },
    computed: {
      // page() {
      //   this.pagelist = this.$store.state.common.pagelist;
      //   var page = 1;
      //   for (let val of this.pagelist) {
      //     if (val.router == "shopAdmin") {
      //       page = val.page;
      //     }
      //   }
      //   return page;
      // },
    },
    methods: {
      handleChange(value) {
        this.shopSelect()
      },

      handleAvatarSuccess(file) {
        this.img = file.data;
      },

      handleSizeChange(val) {
        this.size = val;
        this.shopSelect()
      },
      handleCurrentChange(val) {
        this.page = val;
        // for (let item of this.pagelist) {
        //   if (item.router == "shopAdmin") {
        //     item.page = val;
        //   }
        // }
        // this.$store.commit("common/updataPage", this.pagelist);
        this.shopSelect()
      },
      //搜索
      select() {
        this.shopSelect()
      },
      // 重置
      cleans() {
        this.title = ''
        this.status = ''
        this.classifyId = ''
        this.shopName = ''
        this.shopSelect()
      },
      //发布商品
      addShop() {
        this.$router.push({
          path: '/shopPublish'
        })
      },
      // 修改商品
      shopUpdates(index, rows) {
        this.$router.push({
          path: '/shopAmend',
          query: {
            goodsId: rows.goodsId
          }
        });
      },
      // 删除商品
      shopdeletes(row) {
        this.$confirm(`确定删除此条信息?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('admin/goods/deleteGoodsById'),
            method: 'post',
            params: this.$http.adornParams({
              'goodsId': row.goodsId
            })
          }).then(({
            data
          }) => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.shopSelect()
              }
            })
          })
        }).catch(() => {})
      },
      // 获取商品数据
      shopSelect() {
        let page = this.page
        this.tableDataLoading = true
        this.$http({
          url: this.$http.adornUrl('admin/goods/selectGoodsList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': page,
            'limit': this.size,
            'goodsName': this.title,
            'classifyId': this.classifyId,
            'status': this.status,
            'shopName':this.shopName
          })
        }).then(({
          data
        }) => {
          this.tableDataLoading = false
          for(var i in data.data.list){
            if(data.data.list[i].goodsPicture){
              data.data.list[i].goodsPicture = data.data.list[i].goodsPicture.split(',')
            }

          }
          let returnData = data.data;

          this.tableData = returnData
          this.totalnum = returnData.totalElements
        })
      },
      // 商品分类
      dataSelect() {
        this.tableDataLoading = true
        this.$http({
          url: this.$http.adornUrl('admin/goods/selectAllClassify'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({
          data
        }) => {
          this.tableDataLoading = false
          let returnData = data.data;
          this.classDatas = returnData
        })
      },


      // 修改库存
      kcselect(row, kucun) {
        console.log('修改库存', row, kucun)
        this.$http({
          url: this.$http.adornUrl('admin/goodsShop/updateGoodsByShopId'),
          method: 'post',
          data: this.$http.adornData({
            'goodsId': row.goodsId,
            'shopId': row.shopId,
            'inventory': kucun
          })
        }).then(({
          data
        }) => {
          if (data.code == 0) {
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.shopSelect()
              }
            })
            this.xuanze = []
            this.dialogFormVisible = false
          } else {
            this.$message({
              message: data.msg,
              type: 'warning',
              duration: 1500,
              onClose: () => {}
            })
          }

        })

      },
      // 排序
      paixu(row) {
      	this.$http({
      		url: this.$http.adornUrl('admin/goods/updateSort'),
      		method: 'get',
      		params: this.$http.adornParams({
      			'goodsId': row.goodsId,
      			'sort': row.sort
      		})
      	}).then(({
      		data
      	}) => {
      		if (data.code == 0) {
      			this.$message({
      				message: '操作成功',
      				type: 'success',
      				duration: 1500,
      				onClose: () => {
      					this.shopSelect()
      				}
      			})
      		} else {
      			this.$message({
      				message: '操作成功',
      				type: 'error',
      				duration: 1500,
      				onClose: () => {}
      			})
      		}
      	})
      },
      // 批量修改状态
      transferClcik(id) {
      	if (this.guigeType === '') {
      		this.$message({
      			title: '提示',
      			type: 'error',
      			duration: 1800,
      			message: '请选择状态',
      			type: 'warning'
      		})
      		return
      	}
      	var ids = id ? [id] : this.checkBoxData.map(item => {
      		return item.goodsId
      	})
      	this.$http({
      		url: this.$http.adornUrl('admin/goods/updateBatchStatus'),
      		method: 'get',
      		params: this.$http.adornParams({
      			'goodsIds': ids.toString(),
      			'status': this.guigeType
      		})
      	}).then(({
      		data
      	}) => {
      		if (data.code == 0) {
      			this.$message({
      				message: '操作成功',
      				type: 'success',
      				duration: 1500,
      				onClose: () => {
      					this.shopSelect()
      					this.dialogFormVisibleSxj = false
      				}
      			})
      		} else {
      			this.$message({
      				message: '操作成功',
      				type: 'error',
      				duration: 1500,
      				onClose: () => {}
      			})
      		}
      	})
      },
      // 多选
      changeFun(val) {
      	console.log('val', val)
      	this.checkBoxData = val
      },
      // 上下架弹框
      shanxiajia() {
      	this.guigeType = ''
      	this.dialogFormVisibleSxj = true
      },
      // 批量删除
      deletesPl(id) {
      	var ids = id ? [id] : this.checkBoxData.map(item => {
      		return item.goodsId
      	})
      	this.$http({
      		url: this.$http.adornUrl('admin/goods/deleteBatchIds'),
      		method: 'get',
      		params: this.$http.adornParams({
      			'goodsIds': ids.toString(),
      		})
      	}).then(({
      		data
      	}) => {
      		if (data.code == 0) {
      			this.$message({
      				message: '操作成功',
      				type: 'success',
      				duration: 1500,
      				onClose: () => {
      					this.shopSelect()
      				}
      			})
      		} else {
      			this.$message({
      				message: '操作成功',
      				type: 'error',
      				duration: 1500,
      				onClose: () => {}
      			})
      		}
      	})
      },
      // 批量修改库存弹框
      kiucun() {
      	this.num = ''
      	this.dialogFormVisibleKc = true
      },
      transferClcikKc(id) {
      	var ids = id ? [id] : this.checkBoxData.map(item => {
      		return item.goodsId
      	})
      	this.$http({
      		url: this.$http.adornUrl('admin/goods/updateBatchInventory'),
      		method: 'get',
      		params: this.$http.adornParams({
      			'goodsIds': ids.toString(),
      			'inventory': this.num
      		})
      	}).then(({
      		data
      	}) => {
      		if (data.code == 0) {
      			this.$message({
      				message: '操作成功',
      				type: 'success',
      				duration: 1500,
      				onClose: () => {
      					this.shopSelect()
      					this.dialogFormVisibleKc = false
      				}
      			})
      		} else {
      			this.$message({
      				message: '操作成功',
      				type: 'error',
      				duration: 1500,
      				onClose: () => {}
      			})
      		}
      	})
      },
      // 状态
      change(status, goodsId) {
      	this.$http({
      		url: this.$http.adornUrl('admin/goods/updateBatchStatus'),
      		method: 'get',
      		params: this.$http.adornParams({
      			'goodsIds': goodsId,
      			'status': status
      		})
      	}).then(({
      		data
      	}) => {
      		this.$message({
      			message: '操作成功',
      			type: 'success',
      			duration: 1500,
      			onClose: () => {
      				this.shopSelect()
      			}
      		})
      	})
      },
    },
    mounted() {
      this.shopSelect()

      this.dataSelect()
    }
  };
</script>

<style>

</style>
